#include "windowxp.h"

WindowXP::WindowXP(QWidget *parent) : QMainWindow(parent)
{
    initWindow();
}

void WindowXP::initWindow()
{
    //获取菜单栏
    m_Bar = menuBar();

    //菜单栏上面添加菜单
    m_Bar->addMenu("文件");
    m_Bar->addMenu("编辑");
    m_Bar->addMenu("管理");
    m_Bar->addMenu("设置");

    //菜单栏添加菜单
    file_menu = m_Bar->addMenu("文件");
    edit_menu = m_Bar->addMenu("编辑");

    //文件菜单添加菜单项
    QAction * openAct = file_menu->addAction(QIcon(":/1.jpg"),"达咩");
    //打开菜单项设置快捷键
    openAct->setShortcut(QKeySequence("ctrl+n"));

    //使用qt5的连接，在同一线程中，第五个参数默认autoconnect是直连接
    connect(openAct,&QAction::triggered,this,&WindowXP::openNewWindow);

    //设置中心---多行文本框
    edit = new QTextEdit;
    setCentralWidget(edit);


    //文件菜单添加菜单项
    QAction * undoAct = edit_menu->addAction(QIcon(":/1.jpg"),"撤销");
    //打开菜单项设置快捷键
    undoAct->setShortcut(QKeySequence("ctrl+p"));

    //用快捷键撤销---这里的对象是edit多行文本框对象,不是this
    //connect(undoAct,&QAction::triggered,edit,&QTextEdit::undo);
    //使用this，一样的结果
    connect(undoAct,&QAction::triggered,this,&WindowXP::customUndo);

    //添加分割线
    file_menu->addSeparator();
    file_menu->addAction(QIcon(":/1.jpg"),"test分割线");

    //添加二级菜单
    QMenu* menu2 = edit_menu->addMenu("二级菜单");
    QMenu* menu3 = menu2->addMenu("三级菜单");
    menu3->addAction("编辑测试");


    //设置状态栏
    s_Bar = statusBar();
    s_Bar->showMessage("这是一个状态栏");
    s_Bar->setVisible(false);


    //添加工具栏
    t_Bar1 = addToolBar("toolbar1");
    t_Bar1->addAction(openAct);
    t_Bar1->addAction(undoAct);
    //设置图标和文本同时显示
    t_Bar1->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    addToolBar("toolbar2");
    addToolBar("toolbar3");


    //浮动面板
    QDockWidget * d_window = new QDockWidget ("函数窗口");

    //QPushButton

    addDockWidget (Qt::LeftDockWidgetArea,d_window);
}

//菜单项打开一个新界面
void WindowXP::openNewWindow()
{
    QWidget * w = new QWidget;
    w->setWindowTitle("new window");
    w->show();
}

void WindowXP::customUndo()
{
    edit->undo();
}


